<cfsilent>
	<cfset users = $.run.it('craftivitySErvice.getUserData',{order="user.last_request desc,user.created desc"}) />
	<cfif request.session.user.admin neq 1>
		<cfthrow message="user attempted to access restricted page">
	</cfif>
</cfsilent>

<cfoutput>
	<cfsavecontent variable="page">
		<div class="header variable-width">
			<i class="fa fa-home" onclick="Main.nav('home');"></i>
			<div class="header-title">User MAnagement</div>
			#$.render.img(path="craftivity/widget_icons/lightbulb.png",class="title_logo")#</br>
		</div>
		
		<style>
			##users_table{
				color:##444444;
				text-align:left;
				font-size:11px;
			}
			##users_table td.user_td{
				padding:2px;
				border-top:1px solid ##bbbbbb;
			}
			##users_table th{
				background-color:##dddddd;
			}
			tr.user_row:hover > td.user_td{
				background-color:rgb(236, 255, 224);
			}
		</style>
		<script>
			toggleOptions = function(id,a){
				var $a = $(a);
				var $tr = $('tr[userOptionID='+id+']');
				if($tr.is(':visible')){
					$tr.hide();
					$a.text('show');
				} else {
					$tr.show();
					$a.text('hide');
				}
			}
		</script>
		
		<div class="widget short userList" style="width:1215px;">
			<div class="widget-title">
				Users (ordered by activity)
			</div>
			<table id="users_table" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<th width="1%">##</th>
					<th>User</th>
					<th>Business</th>
					<th>Requested</th>
					<th>Created</th>
					<th>Options</th>
					<th>Agree</th>
					<th>Google</th>
					<th>Event</th>
					<th>Goal</th>
					<th>Week</th>
					<th>Imp</th>
					<th>Unsub</th>
					<th>Emails</th>
				</tr>
				<cfloop query="users">
					<cfset userOptions = deserializeJSON(users.options) />
					<tr class="user_row">
						<td class="user_td">#users.currentRow#</td>
						<td class="user_td">
							<a href="javascript:Main.func('CraftivityController','login','&email=#users.email#&password=#$.format.decrypt(users.password)#','Log in as #users.first_name# #users.last_name#?');">
								#users.first_name# <cfif users.middle_Name neq "">#users.middle_name# </cfif>#users.last_name#
							</a></br>
							#users.email#
						</td>
						<td class="user_td">#users.business_name#
							<cfif users.website neq "">
								<cfset site = users.website />
								<cfif not users.website contains "http">
									<cfset site = 'http://' & site />
								</cfif>
								</br><a href="#site#" target="_blank">#users.website#</a>
							</cfif>
						</td>
						<td class="user_td">
							<cfset last_req = "" />
							<cfif users.last_request neq "">
								<cfset req = $.date.applyLocalization(users.last_request) />
								<cfset last_req = "#$.format.humanizeDate(req)# #$.format.humanizeTime(req)#" />
							</cfif>
							#last_req#
						</td>
						<td class="user_td" title="##">
							<cfif users.created neq "">
								<cfset created = $.date.applyLocalization(users.created) />
								<cfset created_on = "#$.format.humanizeDate(created)# #$.format.humanizeTime(created)#" />
							<cfelse>
								<cfset created = "" />
								<cfset created_on = "No Create Date" />
							</cfif>
							<a title="#created#" href="javascript:Main.func('CraftivityController','terminateUser','&userID=#users.id#','Terminate #users.first_name# #users.last_name#?');">
								#created_on#
							</a>
						</td>
						<td class="user_td"><a href="javscript:void(0);" onclick="toggleOptions('#users.id#',this);">show</a></td>
						<td class="user_td" <cfif users.agreement neq 1>style="color:red;"</cfif>>
							#yesNoFormat(users.agreement)#
						</td>
						<td class="user_td">
							<cfif structKeyExists(userOptions,"calendar") and structKeyExists(userOptions.calendar,"refresh_token") and userOptions.calendar.refresh_token neq "">
								Yes
							<cfelse>
								No
							</cfif>
						</td>
						<td class="user_td">#users.event_count#</td>
						<td class="user_td">#users.goal_count#</td>
						<td class="user_td">#users.workweek_count#</td>
						<td class="user_td">#users.important_count#</td>
						<td class="user_td">
							<cfif users.unsubscribe eq 1>
								<span style="color:red;">Yes</span>
							</cfif>
						</td>
						<td class="user_td">#users.email_count#</td>
					</tr>
					<tr style="display:none;" userOptionID="#users.id#">
						<td colspan="99">
							<cfdump var="#userOptions#">
						</td>
					</tr>
				</cfloop>
			</table>
		</div>
		
		<cfquery name="getSignupList" datasource="#$.site.getVar('datasource')#">
			select 
			signup_email.id,
			signup_email.email,
			date_format(signup_email.created,'%m/%d/%Y %H:%i:%s') as created
			from signup_email 
			left join user on user.email = signup_email.email
				and user.active = 1
			where signup_email.active = 1
			and user.id is null
			order by signup_email.created desc
		</cfquery>
		<div class="widget short signupList" style="text-align:left;">
			<div class="widget-title">
				#getSignupList.recordcount# Signups
			</div>
			<div class="signups selectable">
				<cfloop query="getSignuplist">
					<div style="padding:3px 6px;">
						<cfset created = $.date.applyLocalization(getSignuplist.created) />
						<span class="selectable" style="display:inline-block;width:95px;">#$.format.humanizeDate(created)# #$.format.humanizeTime(created)#</span> 
						<span class="selectable" style="display:inline-block;width:165px;">#getSignupList.email#</span>
					</div>
				</cfloop>
				#$.render.form(name='sendBetaEmails',method='craftivityController.sendBetaEmails',onsubmit="return confirm('send?');")#
					<button type="submit">Send Beta Invites</button>
				</form>
			</div>
		</div>
	</cfsavecontent>
	
	#$.run.it("craftivityView.renderPage",{centerContent=page})#
</cfoutput>